/**
 * 
 */
package com.buss.core.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.dic.MenuType;
import com.buss.core.model.Sysmenu;
import com.buss.core.model.User;
import com.buss.core.persistence.ResultList;
import com.buss.core.service.SysmenuService;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.validator.ResultMessage;

/**
 * 系统菜单Controller
 * 
 * @author 高小勇 创建时间:2011-5-2 wcms2.0
 */
@Controller
@RequestMapping(value = "/admin")
public class SysmenuController extends BaseController {

	@Autowired
	private SysmenuService sysmenuService;

	
	@RequestMapping("/sysmenu/page")
	public ModelAndView listPage(@RequestParam(required=false) String menuid){
		ModelAndView view = new ModelAndView("sysmenu/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}
	
	/**
	 * 新增系统菜单信息
	 * @param sysmenu
	 * @return
	 */
	@RequestMapping("/sysmenu/add")
	@ResponseBody
	public Map<String,Object> add( Sysmenu sysmenu){
		Sysmenu tmpSysmenu = sysmenuService.mergeSysMenu(sysmenu);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysmenu", tmpSysmenu);
		map.put("message", new ResultMessage("0",  "新增系统菜单信息成功!"));
		return map;
	}
	
	/**
	 * 修改系统菜单信息
	 * @param sysmenu
	 * @return
	 */
	@RequestMapping("/sysmenu/edit")
	@ResponseBody
	public Map<String, Object> edit( Sysmenu sysmenu){
		sysmenuService.editSysMenu(sysmenu);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0","修改系统菜单信息成功!"));
		return map;
	}
	
	/**
	 * 删除系统菜单信息
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysmenu/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Long[] ids){
		sysmenuService.deleteMenu(ids);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message",new ResultMessage("0", "删除系统菜单信息成功!"));
		return map;
	}
	
	@RequestMapping("/sysmenu/{type}")
	public ModelAndView editPage(@PathVariable String type,@RequestParam(required=false) Long id){
		ModelAndView view = new ModelAndView("sysmenu/edit");
		Sysmenu sysmenu = new Sysmenu();
		Sysmenu parentSysmenu = null;
		if("editpage".equals(type)){
			sysmenu = sysmenuService.loadSysMenu(id);
			if(sysmenu.getParentId() != 0){
				parentSysmenu = sysmenuService.loadSysMenu(sysmenu.getParentId());
			}
		}else{
			if(id != 0){
				parentSysmenu = sysmenuService.loadSysMenu(id);
			}
			sysmenu.setMenuType(MenuType.menu);
		}
		if(parentSysmenu == null){
			parentSysmenu = new Sysmenu();
			parentSysmenu.setId(0L);
			parentSysmenu.setName("系统菜单信息");			
		}
		view.addObject("pSysmenu", parentSysmenu);
		view.addObject("sysmenu", sysmenu);
		return view;
	}
	
	/**
	 * 根据系统菜单编码获取子菜单信息列表
	 * @param parentId
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysmenu/list/{parentId}")
	@ResponseBody
	public Map<String, Object> list(@PathVariable Long parentId,
			HttpServletRequest request) {
		ResultList<Sysmenu> resultList = sysmenuService.querySysMenu(parentId,
				null,null);
		ResultMessage resultMessage = new ResultMessage("0", "获取系统菜单信息列表成功!");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", resultList);
		map.put("message", resultMessage);
		return map;
	}

	
	/**
	 * 根据菜单编码获取指定用户角色对应的可用菜单信息列表 
	 * @param parentId
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/sysmenu/listByRole/{parentId}")
	@ResponseBody
	public Map<String, Object> listByRole(@PathVariable Long parentId,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		ResultList<Sysmenu> resultList = null;
		User user = this.getUser(request);
		if(user.isSysAdmin()){
			resultList = sysmenuService.querySysMenu(parentId, MenuType.menu,false);
			queryChild(resultList.getData(),MenuType.menu,false);
		}else{
			resultList = sysmenuService.querySysMenuByRole(
				this.getUserRoleIds(request), parentId, MenuType.menu,false, null);
			qyertMenuByRolesChild(resultList,MenuType.menu,this.getUserRoleIds(request));			
		}
		map.put("result", resultList);
		map.put("message", new ResultMessage("0", "获取系统菜单信息成功!"));
		return map;
	}
	
	@RequestMapping("/sysmenu/listMenuByRoles/{parentId}")
	@ResponseBody
	public Map<String,Object> listMenuByRoles(@PathVariable Long parentId, @RequestParam(required=false) MenuType menuType,@RequestParam(required=true) Long roleId, HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		Long[] roleIds = new Long[]{roleId};
		ResultList<Sysmenu> resultList = sysmenuService.queryAllMenuenuByRole(roleIds, parentId, menuType, false, null);
		qyertMenuByRolesChild(resultList,menuType,roleIds);
		map.put("result", resultList);
		map.put("message", new ResultMessage("0", "获取菜单树成功!"));
		return map;
	}
	
	
	/**
	 * 递归获取子所有子菜单信息列表
	 * @param sysmenus
	 * @param roleSysmenuMap
	 * @return
	 */
	private void qyertMenuByRolesChild(ResultList<Sysmenu> sysmenus,MenuType menuType,Long[] roleIds){
		for(Sysmenu sysmenu : sysmenus.getData()){
			ResultList<Sysmenu> resultList = sysmenuService.queryAllMenuenuByRole(roleIds, sysmenu.getId(), menuType, false, null);
			sysmenu.setNodes(resultList.getData());
			qyertMenuByRolesChild(resultList,menuType,roleIds);
		}
	}
	
	
	/**
	 * 获取指定系统菜单下的所有菜单信息
	 * @param parentId
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysmenu/listByTree/{parentId}")
	@ResponseBody
	public Map<String, Object> listByTree(@PathVariable Long parentId, @RequestParam(required=false) MenuType menuType, HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		ResultList<Sysmenu> resultList = sysmenuService.querySysMenu(parentId, menuType,null);
		queryChild(resultList.getData(),menuType,null);
		map.put("result", resultList);
		map.put("message", new ResultMessage("0", "获取菜单树成功!"));
		return map;
	}
	
	/**
	 * 递归获取子所有子菜单信息列表
	 * @param sysmenus
	 * @param roleSysmenuMap
	 * @return
	 */
	private void queryChild(List<Sysmenu> sysmenus,MenuType menuType,Boolean disabled){
		for(Sysmenu sysmenu : sysmenus){
			ResultList<Sysmenu> resultList = sysmenuService.querySysMenu(sysmenu.getId(), menuType,disabled);
			sysmenu.setNodes(resultList.getData());
			queryChild(resultList.getData(),menuType,disabled);
		}
	}

	
	
}
